package com.example.service.impl;

import com.example.mapper.ShiftPhaseMapper;
import com.example.pojo.Shift;
import com.example.mapper.ShiftMapper;
import com.example.pojo.ShiftPhase;
import com.example.service.IShiftService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.vo.per.ShiftParameterSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 班次表 服务实现类
 * </p>
 *
 * @author wsl
 * @since 2022-04-14
 */
@Service
public class ShiftServiceImpl extends ServiceImpl<ShiftMapper, Shift> implements IShiftService {

    @Autowired
    private ShiftMapper sm;
    @Autowired
    private ShiftPhaseMapper spm;
    @Autowired
    private ShiftPhaseServiceImpl spsi;


    @Override
    @Transactional
    public Boolean savess(ShiftParameterSet ps) {
        Shift shift = ps.getShift();
        shift.setCreateTime(LocalDateTime.now());
        int i = sm.insert(shift);

        System.err.println("phase ---> " + ps.getShiftPhase());
        if (i == 1){
            List<ShiftPhase> list = ps.getShiftPhase();
            for (ShiftPhase phase : list) {
                phase.setShiftId(shift.getId());
                phase.setCreateTime(LocalDateTime.now());
                spm.insert(phase);
            }
            return true;
        }
        return false;
    }

    @Override
    public List<Map<String, Object>> selByUncond() {
        return sm.selByUnconditional();
    }

    @Override
    @Transactional
    public Boolean delAll(Integer[] integers) {
        for (Integer i : integers) {
            Shift shift = new Shift();
            shift.setId(i);
            sm.deleteById(shift);
        }
        spsi.delAll(integers);
        return true;
    }

}
